package com.byq.ai.agentlab.controller;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("access")
public class LoginController {
    @GetMapping("login")
    public String login(String userName, String password){

        List<String> permissions = new ArrayList<>();
        permissions.add("chat");
        Collection<GrantedAuthority> authorities = authorities(permissions);
        Authentication authentication = new UsernamePasswordAuthenticationToken(userName, password, authorities);





        SecurityContext scontext = SecurityContextHolder.getContext();

        scontext.setAuthentication(authentication);

        return "success";

    }


    Collection<GrantedAuthority> authorities(List<String> permissions){
        Collection<GrantedAuthority> authorities = new ArrayList();// authentication.getAuthorities();

        for (String p : permissions) {
            GrantedAuthority aut = new SimpleGrantedAuthority(p);
            authorities.add(aut);

        }
        return authorities;


    }
}
